import { message, Modal } from 'antd';
import { WeaTools, WeaDialog } from 'ecCom';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

export const fnJoinPara = (fn, para) => {
  if (!fn || !para || typeof (fn) !== 'string') { return fn; }
  const _st = fn.indexOf('(');
  const _ed = fn.lastIndexOf(')');
  if (_st == -1 || _ed == -1) { return fn; }
  const _stStr = fn.substring(0, _st);
  const _edStr = fn.substring(_ed);
  let _para = fn.substring(_st, _ed);
  _para += `${_para == '(' ? '' : ','}'${para}'`;
  return _stStr + _para + _edStr;
};

// 查询客户   导出客户
export const crmExport = module => WeaTools.callApi('/api/crm/customer/crmExport', 'GET', {}).then((data) => {
  window.location = '/weaver/weaver.file.ExcelOut';
});

// 发送邮件 ---查询客户里面的operate
export const opNewEmail = (email) => {
  const url = `/email/new/MailInBox.jsp?opNewEmail=1&isInternal=0&to=${email}`;
  openFullWindowHaveBar(url);
};

// 新建流程
export const opNewWF = (id) => {
  const url = `/spa/workflow/index_mobx.html#/main/workflow/add?hasStruct=true&crmid=${id}`;
  openFullWindowHaveBar(url);
};

// 发送邮件
export const sendMailBatch = module => WeaTools.callApi('/api/crm/customer/getEmail', 'GET', { customerIds: module }).then((data) => {
  const url = `/spa/email/static/index.html#/main/email/new?opNewEmail=1&isInternal=0&to=${data.datas}`;
  openFullWindowHaveBar(url);
});

// 发送邮件
export const shareBatch = module => WeaTools.callApi('/api/crm/customer/getEmail', 'GET', { customerIds: module }).then((data) => {
  const url = `/email/new/MailInBox.jsp?opNewEmail=1&isInternal=0&to=${data.datas}`;
  openFullWindowHaveBar(url);
});

// 批量删除
export const deleteBatch = (params) => {
  confirm({
    title: getLabel(15172, '系统提示'),
    content: getLabel(83600, '您确认要删除吗？'),
    okText: getLabel(33703, '确定'),
    cancelText: getLabel(201, '取消'),
    onOk() {
      return WeaTools.callApi('/api/crm/customer/crmDelete', 'GET', { customerIds: params }).then((data) => {
        if (data.status == 'success') {
          //  window.store_e9_element.dispatch(SearchAction.doSearch());
          window._crmTable.reLoad();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    },
    onCancel() {},
  });
};

// 升级
export const doLevelUp = params => WeaTools.callApi('/api/crm/customer/crmChangeStatus', 'GET', { customerIds: params, operation: 'up' }).then((data) => {
  if (data.status == 'success') {
    const arr = window._crmTable.getSelectedKeys();
    window._crmTable.reLoad();
    window._crmTable.setSelectedKeys(arr);
    return true;
  }
  message.error(getLabel(83437, '出现错误！') + data.msgcode);
});
// 降级
export const doLevelDown = params => WeaTools.callApi('/api/crm/customer/crmChangeStatus', 'GET', { customerIds: params, operation: 'down' }).then((data) => {
  if (data.status == 'success') {
    const arr = window._crmTable.getSelectedKeys();
    window._crmTable.reLoad();
    window._crmTable.setSelectedKeys(arr);
    return true;
  }
  message.error(getLabel(83437, '出现错误！') + data.msgcode);
});

